package com.sora.sora.server.controller;

import com.sora.sora.common.model.param.CommentParam;
import com.sora.sora.common.model.param.ReplyParam;
import com.sora.sora.common.model.vo.CommentVO;
import com.sora.sora.common.model.vo.ReplyVO;
import com.sora.sora.common.model.vo.ResultVO;
import com.sora.sora.server.service.CommentService;
import com.sora.sora.common.utils.ResponseResult;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "评论模块", tags = "评论管理模块")
@RestController
@RequestMapping("comment")
public class CommentController {

    @Resource
    CommentService commentService;

    @Resource
    ResponseResult responseResult;


    @ApiOperation(value = "用户发表评论")
    @ApiResponses({
            @ApiResponse(code = 201, message = "评论成功，并返回数据"),
            @ApiResponse(code = 1002, message = "评论失败，请重试")
    })
    @PostMapping()
    public ResultVO comment(@RequestBody @Validated(CommentParam.CreateComment.class) CommentParam commentParam) {
        CommentVO vo = commentService.comment(commentParam);
        return vo != null ? responseResult.ok(vo) : responseResult.faile();
    }


    @ApiOperation("用户进行回复")
    @ApiResponses({
            @ApiResponse(code = 201, message = "回复成功，并返回数据"),
            @ApiResponse(code = 1002, message = "回复失败，请重试")
    })
    @PostMapping("/reply")
    public ResultVO reply(@RequestBody @Validated(ReplyParam.CreateReply.class) ReplyParam replyParam) {
        ReplyVO vo = commentService.reply(replyParam);
        return vo != null ? responseResult.ok(vo) : responseResult.faile();
    }


    @ApiOperation("用户删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uid", value = "执行操作的用户id", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 1002, message = "删除失败")
    })
    @DeleteMapping()
    public ResultVO deleteComment(int id, int uid) {
        if (id <= 0 || uid <= 0) {
            return responseResult.valid("非法入参");
        }
        boolean flag = commentService.deleteComment(id, uid);
        return flag ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation("用户删除回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回复id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uid", value = "执行操作的用户id", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 1002, message = "删除失败")
    })
    @DeleteMapping("/reply")
    public ResultVO deleteReply(int id, int uid) {
        if (id <= 0 || uid <= 0) {
            return responseResult.valid("非法入参");
        }
        boolean flag = commentService.deleteReply(id, uid);
        return flag ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation("按时间升序展示资源下的所有评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleType", value = "资源所属分区", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "articleId", value = "资源id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponse(code = 201, message = "操作成功")
    @GetMapping("/{articleType}")
    public ResultVO listCommentTimeAsc(@PathVariable("articleType") String articleType,
                                       @RequestParam("articleId") int articleId,
                                       @RequestParam("page") int page,
                                       @RequestParam("limit") int limit) {
        if (articleId <= 0) {
            return responseResult.valid("非法入参");
        }
        return responseResult.ok(commentService.listAllCommentAscTime(articleType, articleId, page, limit));
    }


    @ApiOperation("按时间降序展示资源下的所有评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleType", value = "资源所属分区", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "articleId", value = "资源id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponse(code = 201, message = "操作成功")
    @GetMapping("/{articleType}/")
    public ResultVO listCommentTimeDesc(@PathVariable("articleType") String articleType,
                                       @RequestParam("articleId") int articleId,
                                       @RequestParam("page") int page,
                                       @RequestParam("limit") int limit) {
        if (articleId <= 0) {
            return responseResult.valid("非法入参");
        }
        return responseResult.ok(commentService.listAllCommentDescTime(articleType, articleId, page, limit));
    }


    @ApiOperation("按时间升序展示评论下的所有回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "回复的评论id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponse(code = 201, message = "操作成功")
    @GetMapping("/reply")
    public ResultVO listReplyTimeAsc(@RequestParam("commentId") int commentId,
                                     @RequestParam("page") int page,
                                     @RequestParam("limit") int limit) {
        if (commentId <= 0) {
            return responseResult.valid("非法入参");
        }
        return responseResult.ok(commentService.listAllReplyAscTime(commentId, page, limit));
    }


    @ApiOperation("按时间降序展示评论下的所有回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "回复的评论id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponse(code = 201, message = "操作成功")
    @GetMapping("/reply/")
    public ResultVO listReplyTimeDesc(@RequestParam("commentId") int commentId,
                                     @RequestParam("page") int page,
                                     @RequestParam("limit") int limit) {
        if (commentId <= 0) {
            return responseResult.valid("非法入参");
        }
        return responseResult.ok(commentService.listAllReplyDescTime(commentId, page, limit));
    }


    @ApiOperation("获得评论下的回复总数")
    @ApiImplicitParam(name = "commentId", value = "评价id", required = true, dataType = "int", paramType = "query")
    @GetMapping("/reply/count")
    public ResultVO getCount(int commentId) {
        if (commentId <= 0) {
            return responseResult.valid("非法入参");
        }
        return responseResult.ok(commentService.countReply(commentId));
    }
}
